//
//  CLDataModel.m
//  chanlin
//
//  Created by Ryan on 14-11-1.
//  Copyright (c) 2014年 chanlin. All rights reserved.
//

#import "CLDataModel.h"
#import "AFNetworking.h"
#import "SgrGCD.h"
#import "Sgr_macro.h"
#import "CLGloble.h"
#import "NSObject+sagittarius.h"

@implementation CLDataModel

//SGR_DEF_SINGLETION(CLDataModel)

- (void)getWithUrl:(NSString *)url
             param:(NSDictionary *)param
         dataBlock:(void (^)(BOOL success,id responseObj,NSError *message,NSMutableDictionary *param) )block1
           uiBlock:(void (^)(BOOL success,id responseObj,NSError *message,NSMutableDictionary *param) )block2{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    
    NSMutableDictionary *p=[NSMutableDictionary dictionary];
    if(param){
        [p setDictionary:param];
    }
    NSString *v=[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    [p setObject:v forKey:@"gv"];
    [p setObject:@"ios" forKey:@"os"];
    
    [manager GET:url parameters:p success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *params=[NSMutableDictionary dictionary];
        if(block1){
            [[SgrGCD sharedInstance] enqueueGloble:^{
                block1(YES,responseObject,nil,params);
                sgrSafeMainThread(^{
                    block2(YES,responseObject,nil,params);
                }
                );
            }];
        }else if(block2){
            block2(YES,responseObject,nil,params);
        }
       
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSMutableDictionary *params=[NSMutableDictionary dictionary];
        if(block1){
            [[SgrGCD sharedInstance] enqueueGloble:^{
                

                block1(NO,nil,error,params);
                sgrSafeMainThread(^{
                    block2(NO,nil,error,params);
                }
                );
            }];
        }else if(block2){
            block2(NO,nil,error,params);
        }

    }];
}

- (NSString *)urlWithCategory:(NSString *)key{
    return [[CLGloble sharedInstance].urls sgrGetStringForKey:key];
}

- (void)postWithUrl:(NSString *)url
              param:(NSDictionary *)param
          dataBlock:(void (^)(BOOL success,id responseObj,NSError *message,NSMutableDictionary *param) )block1
            uiBlock:(void (^)(BOOL success,id responseObj,NSError *message,NSMutableDictionary *param) )block2{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    
    NSMutableDictionary *p=[NSMutableDictionary dictionary];
    if(param){
        [p setDictionary:param];
    }
    NSString *v=[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    [p setObject:v forKey:@"gv"];
    [p setObject:@"ios" forKey:@"os"];
    
    [manager POST:url parameters:p success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *params=[NSMutableDictionary dictionary];
        if(block1){
            [[SgrGCD sharedInstance] enqueueGloble:^{
                block1(YES,responseObject,nil,params);
                sgrSafeMainThread(^{
                    block2(YES,responseObject,nil,params);
                }
                                  );
            }];
        }else if(block2){
            block2(YES,responseObject,nil,params);
        }

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSMutableDictionary *params=[NSMutableDictionary dictionary];
        if(block1){
            [[SgrGCD sharedInstance] enqueueGloble:^{
                
                
                block1(NO,nil,error,params);
                sgrSafeMainThread(^{
                    block2(NO,nil,error,params);
                }
                                  );
            }];
        }else if(block2){
            block2(NO,nil,error,params);
        }

    }];
}

@end
